using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.CompanyDomain;
//using ShivamEMR.Web.Areas.Company.Models;
using ShivamEMR.Model.DataInterfaces.CompanyDataInterf;
using ShivamEMR.Model;
using ShivamEMR.Web.Models;

namespace ShivamEMR.Web.Areas.Company.Controllers
{   
    public class CompanyController : Controller
    {
		private readonly ICompanyGroupRepository companygroupRepository;
		private readonly ICompanyPictureRepository companypictureRepository;
		private readonly ICompanyRepository companyRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public CompanyController() 
        {
            companygroupRepository = Registry.RepositoryFactory.GetCompanyGroupRepository();
            companypictureRepository = Registry.RepositoryFactory.GetCompanyPictureRepository();
            companyRepository = Registry.RepositoryFactory.GetCompanyRepository();
        }

        public CompanyController(ICompanyGroupRepository companygroupRepository, ICompanyPictureRepository companypictureRepository, ICompanyRepository companyRepository)
        {
			this.companygroupRepository = companygroupRepository;
			this.companypictureRepository = companypictureRepository;
			this.companyRepository = companyRepository;
        }

        //
        // GET: /Company/
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Index()
        {
            //return View(companyRepository.AllIncluding(company => company.BranchGroups, company => company.Branches));
            return View(companyRepository.All);
        }

        //
        // GET: /Company/Details/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Details(int id)
        {
            return View(companyRepository.Find(id));
        }

        //
        // GET: /Company/Create
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Create()
        {
			ViewBag.PossibleCompanyGroups = companygroupRepository.All;
			ViewBag.PossibleCompanyPictures = companypictureRepository.All;
            return View();
        } 

        //
        // POST: /Company/Create
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Create(ShivamEMR.Model.Domain.CompanyDomain.Company company)
        {
            if (ModelState.IsValid) {
                company.CompanyPictureID = Guid.Parse( "08ED5341-C789-4710-AA74-1137BB363B49");
                companyRepository.Create(company);
                companyRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleCompanyGroups = companygroupRepository.All;
				ViewBag.PossibleCompanyPictures = companypictureRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Company/Edit/5
 [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Edit(int id)
        {
			ViewBag.PossibleCompanyGroups = companygroupRepository.All;
			ViewBag.PossibleCompanyPictures = companypictureRepository.All;
             return View(companyRepository.Find(id));
        }

        //
        // POST: /Company/Edit/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Edit(ShivamEMR.Model.Domain.CompanyDomain.Company company)
        {
            if (ModelState.IsValid) {
                companyRepository.Update(company);
                companyRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleCompanyGroups = companygroupRepository.All;
				ViewBag.PossibleCompanyPictures = companypictureRepository.All;
				return View();
			}
        }

        //
        // GET: /Company/Delete/5
 [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Delete(int id)
        {
            return View(companyRepository.Find(id));
        }

        //
        // POST: /Company/Delete/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
           // companyRepository.Delete(id);
            var comp = companyRepository.Find(id);
            companyRepository.Delete(comp);
            companyRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

